﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SudokuGame.Sudoku
{
    public class RemarkAttribute : Attribute
    {
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }

        public RemarkAttribute(string remark)
        {
            this.Remark = remark;
        }
    }
    public static class EnumEx
    {
        /// <summary>
        /// 根据枚举元素,获取该枚举元素的描述信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tField"></param>
        /// <returns></returns>
        public static string GetEnumDesc<T>(this T tField) where T : Enum
        {
            var description = string.Empty; //结果
            var inputType = tField.GetType(); //输入的类型
            var descType = typeof(RemarkAttribute); //目标查找的描述类型

            var fieldStr = tField.ToString();                //输入的字段字符串
            var field = inputType.GetField(fieldStr);        //目标字段

            var isDefined = field?.IsDefined(descType, false);//判断描述是否在字段的特性
            if (isDefined ?? false)
            {
                var enumAttributes = field.GetCustomAttributes(descType, false) as RemarkAttribute[];  //得到特性信息
                description = enumAttributes?.FirstOrDefault()?.Remark ?? string.Empty;
                //  description = string.Join(',', enumAttributes?.Select(t => t.Remark));
            }
            return description;
        }
    }
}
